package com.woniuxy.tfss.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.tfss.entity.Information;
import com.woniuxy.tfss.entity.ResponseEntity;
import com.woniuxy.tfss.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("information")
public class InformationController {

    @Autowired
    private InformationService informationService;

    @GetMapping("page")
    public String info(){
        return "information";
    }

    @GetMapping("list/{recType}/{recId}")
    @ResponseBody
    public ResponseEntity<?> getInformations(@PathVariable String recType,@PathVariable Integer recId){
        List<Information> infos = new ArrayList<>();
        if(recType.equals(Information.TOUSER)){
            infos=informationService.getByUserId(recId);
        }else if(recType.equals(Information.TORIDER)){
            infos=informationService.getByRiderId(recId);
        }
        return new ResponseEntity<>(infos);
    }

    @GetMapping("read/{id}")
    @ResponseBody
    public ResponseEntity<?> read(@PathVariable Integer id){
        Information info=informationService.getById(id);
        info.setSeen(Information.READ);
        informationService.updateById(info);
        return ResponseEntity.SUCCESS;
    }

    @GetMapping("del/{id}")
    @ResponseBody
    public ResponseEntity<?> deleteInfo(@PathVariable Integer id){
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        informationService.remove(queryWrapper);
        return ResponseEntity.SUCCESS;
    }
}
